package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjGoodsUnitMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjGoodsUnitService;
import com.zb.service.ZjProductService;
import com.zb.service.ZjSpareService;
import com.zb.vo.GoodsUnitListVo;
import com.zb.vo.GoodsUnitSelectVo;
import com.zb.vo.GoodsUnitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品单位表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Service
public class ZjGoodsUnitServiceImpl extends ServiceImpl<ZjGoodsUnitMapper, ZjGoodsUnit> implements ZjGoodsUnitService {
    @Resource
    private ZjGoodsUnitMapper goodsUnitMapper;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjSpareService spareService;

    // 新增或修改单位
    @Override
    public void save(Long custAccId, GoodsUnitVo unitVo) {
        Assert.isTrue(null == goodsUnitMapper.existByName(custAccId, unitVo.getId(), unitVo.getName()), "单位名称已经重复", unitVo.getName());
        ZjGoodsUnit unit = new ZjGoodsUnit();
        BeanUtils.copyProperties(unitVo, unit);
        unit.setDisabled(unitVo.getStatus());
        if (null != unit.getId()) {
            this.updateById(unit);
            productService.updateUnitByUnitId(unit.getId(), unit.getName());
            spareService.updateUnitByUnitId(unit.getId(), unit.getName());
            return;
        }
        unit.setId(IdWorker.getId());
        unit.setCustAccId(custAccId);
        this.save(unit);
    }

    // 获取商品单位列表
    @Override
    public Map<String, Object> getList(Long custAccId, GoodsUnitSelectVo selectVo) {
        QueryWrapper<ZjGoodsUnit> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjGoodsUnit> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);

        List<GoodsUnitListVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(GoodsUnitListVo.beanToVo(t)));

        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 批量禁用或启用商品单位
    @Override
    public void updateBatchDisalbeByIds(List<Long> idList, boolean disable) {
        goodsUnitMapper.updateBatchDisalbeByIds(idList, disable);
    }

    // 批量删除商品单位
    @Override
    public void delete(List<Long> idList) {
        List<ZjProduct> productList = productService.getRelationUnitByUnitIds(idList);
        com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(CollectionUtil.isEmpty(productList),
                "删除的商品单位[ "+ productList.stream().map(ZjProduct::getUnit).collect(Collectors.joining("、"))
                        +" ]已与产品型号[ "+ productList.stream().map(ZjProduct::getModel).collect(Collectors.joining("、")) +" ]关联使用中，不允许删除");
        List<ZjSpare> spareList = spareService.getRelationUnitByUnitIds(idList);
        com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(CollectionUtil.isEmpty(spareList),
                "删除的商品单位[ "+ spareList.stream().map(ZjSpare::getUnit).collect(Collectors.joining("、"))
                        +" ]已与备件型号[ "+ spareList.stream().map(ZjSpare::getModel).collect(Collectors.joining("、")) +" ]关联使用中，不允许删除");
        this.removeByIds(idList);
    }

    // 分页获取单位下拉数据列表
    @Override
    public Map<String, Object> pageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjGoodsUnit> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjGoodsUnit> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(new SelectResultVo(t.getId(), t.getName())));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
